package learn.basic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.gephi.data.attributes.api.AttributeColumn;
import org.gephi.data.attributes.api.AttributeController;
import org.gephi.data.attributes.api.AttributeModel;
import org.gephi.graph.api.DirectedGraph;
import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphModel;
import org.gephi.io.exporter.api.ExportController;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.EdgeDefault;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.processor.plugin.DefaultProcessor;
import org.gephi.partition.api.Partition;
import org.gephi.partition.api.PartitionController;
import org.gephi.partition.plugin.NodeColorTransformer;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.gephi.statistics.plugin.Modularity;
import org.openide.util.Lookup;

public class ManipulatePartition {

	@SuppressWarnings("rawtypes")
	public static void main(String[] args) {
		ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
		pc.newProject();
		Workspace workspace = pc.getCurrentWorkspace();
		
		ImportController importController = Lookup.getDefault().lookup(ImportController.class);
		GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
		AttributeModel attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel();
		
		Container container;
		try {
			File file = new File("Data/polblogs.gml");
			container = importController.importFile(file);
			container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}
		
		importController.process(container, new DefaultProcessor(), workspace);
		
		DirectedGraph graph = graphModel.getDirectedGraph();
		System.out.println("Nodes: " + graph.getNodeCount());
		System.out.println("Edges: " + graph.getEdgeCount());
		
		PartitionController partitionController = Lookup.getDefault().lookup(PartitionController.class);
		Partition p = partitionController.buildPartition(attributeModel.getNodeTable().getColumn("source"), graph);
		NodeColorTransformer nodeColorTransformer = new NodeColorTransformer();
		nodeColorTransformer.randomizeColors(p);
		
		ExportController ec = Lookup.getDefault().lookup(ExportController.class);
		try {
			ec.exportFile(new File("d://partition1.pdf"));
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		
		Modularity modularity = new Modularity();
		modularity.execute(graphModel, attributeModel);
		
		AttributeColumn modColumn = attributeModel.getNodeTable().getColumn(Modularity.MODULARITY_CLASS);
		Partition p2 = partitionController.buildPartition(modColumn, graph);
		System.out.println(p2.getPartsCount() + " partitions found");
		NodeColorTransformer nodeColorTransformer2 = new NodeColorTransformer();
		nodeColorTransformer2.randomizeColors(p2);
		partitionController.transform(p2, nodeColorTransformer2);
		
		try {
			ec.exportFile(new File("d://partition2.pdf"));
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
	}

}